package flc.ast.dialog;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.CameraActivity;
import flc.ast.activity.SelCameraActivity;
import flc.ast.adapter.SelPictureAdapter;
import flc.ast.adapter.SelVideoAdapter;
import flc.ast.databinding.DialogChangeCoverBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.base.BaseSmartDialog;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class ChangeCoverDialog extends BaseSmartDialog<DialogChangeCoverBinding> implements View.OnClickListener {

    private boolean isVideo = false;
    private SelPictureAdapter pictureAdapter;
    private SelVideoAdapter videoAdapter;
    private String selPath = "";
    private int oldPosition;

    public ChangeCoverDialog(@NonNull Context context, boolean isVideo) {
        super(context);
        this.isVideo = isVideo;
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_change_cover;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvDialogChangeCoverConfirm.setOnClickListener(this);
        mDataBinding.tvDialogChangeCoverCancel.setOnClickListener(this);

        mDataBinding.rvPictureList.setLayoutManager(new GridLayoutManager(getContext(), 4));
        pictureAdapter = new SelPictureAdapter();
        pictureAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (position == 0) {
                    SelCameraActivity.type = 0;
                    Intent intent = new Intent(getContext(), SelCameraActivity.class);
                    getContext().startActivity(intent);
                    return;
                }
                pictureAdapter.getItem(oldPosition).setChecked(false);
                pictureAdapter.notifyItemChanged(oldPosition);
                oldPosition = position;
                pictureAdapter.getItem(position).setChecked(true);
                pictureAdapter.notifyItemChanged(position);
                selPath = pictureAdapter.getItem(position).getPath();
            }
        });

        videoAdapter = new SelVideoAdapter();
        videoAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (position == 0) {
                    SelCameraActivity.type = 1;
                    Intent intent = new Intent(getContext(), SelCameraActivity.class);
                    getContext().startActivity(intent);
                    return;
                }
                videoAdapter.getItem(oldPosition).setChecked(false);
                videoAdapter.notifyItemChanged(oldPosition);
                oldPosition = position;
                videoAdapter.getItem(position).setChecked(true);
                videoAdapter.notifyItemChanged(position);
                selPath = videoAdapter.getItem(position).getPath();
            }
        });

        if (isVideo) {
            getVideoPermission();
        } else {
            getPermission();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvDialogChangeCoverConfirm:
                listener.clickConfirm(selPath);
                dismiss();
                break;
            case R.id.tvDialogChangeCoverCancel:
                dismiss();
                break;
        }
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO).reqPermissionDesc(getContext().getString(R.string.get_picture_permission)).callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                getData();
            }
        }).request();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(getContext(), PhotoConfig.Mode.PHOTO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    selectMediaEntities.add(0, new SelectMediaEntity());
                    pictureAdapter.setList(selectMediaEntities);
                    mDataBinding.rvPictureList.setAdapter(pictureAdapter);
                    mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
                    mDataBinding.tvPictureNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvPictureList.setVisibility(View.GONE);
                    mDataBinding.tvPictureNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private void getVideoPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO).reqPermissionDesc(getContext().getString(R.string.get_video_permission)).callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                getVideoData();
            }
        }).request();
    }

    //获取视频和图片数据
    private void getVideoData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(getContext(), PhotoConfig.Mode.VIDEO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    selectMediaEntities.add(0, new SelectMediaEntity());
                    videoAdapter.setList(selectMediaEntities);
                    mDataBinding.rvPictureList.setAdapter(videoAdapter);
                    mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
                    mDataBinding.tvPictureNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvPictureList.setVisibility(View.GONE);
                    mDataBinding.tvPictureNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }


    private Listener listener;

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public interface Listener {
        void clickConfirm(String path);
    }
}
